package com.mou.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mou.constant.MessageConstant;
import com.mou.context.BaseContext;
import com.mou.dto.PaymentDTO;
import com.mou.entity.Payment;
import com.mou.entity.Product;
import com.mou.entity.Transaction;
import com.mou.entity.User;
import com.mou.exception.PaymentException;
import com.mou.exception.TransactionOrderException;
import com.mou.mapper.PaymentMapper;
import com.mou.mapper.ProductMapper;
import com.mou.mapper.TransactionMapper;
import com.mou.mapper.UserMapper;
import com.mou.service.PaymentService;
import com.mou.utils.Md5Util;
import com.mou.vo.OrderInfoVO;
import com.mou.vo.PayMethodsVO;
import com.mou.vo.PaymentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("paymentServiceImpl")
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, Payment> implements PaymentService {
    @Autowired
    private PaymentMapper paymentMapper;
    @Autowired
    private TransactionMapper transactionMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public OrderInfoVO getOrder(Integer id) {
        if (id != null) {
            OrderInfoVO orderInfoVO = new OrderInfoVO();
            Transaction transaction = transactionMapper.getOrderById(id);
            Product product = productMapper.selectById(transaction.getProductId());
            User user = userMapper.selectById(transaction.getBuyerId());
            if (product != null) {
                orderInfoVO.setAmount(product.getPrice() * transaction.getQuantity());
                orderInfoVO.setOrderId(transaction.getId());
                orderInfoVO.setExpireTime(transaction.getTransactionDate().plusMinutes(30));
                orderInfoVO.setBalance(user.getBalance());
            } else {
                throw new TransactionOrderException(MessageConstant.ORDER_NOT_EXIST);
            }
            return orderInfoVO;
        } else {
            throw new TransactionOrderException(MessageConstant.ORDER_NOT_EXIST);
        }
    }

    @Override
    public List<PayMethodsVO> getMethods() {
        List<PayMethodsVO> payMethodsVOS = paymentMapper.getMethods();
        return payMethodsVOS;
    }

    @Override
    @Transactional
    public PaymentVO qrcode(PaymentDTO paymentDTO) {
        Payment payment = paymentMapper.selectById(paymentDTO.getId());
        if (payment.getLabel().equals("微信支付")) {
            PaymentVO paymentVO = new PaymentVO();
            paymentVO.setQrcodeUrl("https://wangmou-super.oss-cn-beijing.aliyuncs.com/21839755-f60b-4e84-ac55-7d2ff1e34496.png");
            return paymentVO;
        } else if (payment.getLabel().equals("支付宝")) {
            PaymentVO paymentVO = new PaymentVO();
            paymentVO.setQrcodeUrl("https://wangmou-super.oss-cn-beijing.aliyuncs.com/fe48d6c9-63fe-4148-8d11-ffa7588eb69c.png");
            return paymentVO;
        } else if (payment.getLabel().equals("余额支付")) {
            Integer currentUserId = BaseContext.getCurrentId();
            User user = userMapper.selectById(currentUserId);
            String md5PayPwd = Md5Util.getMD5String(paymentDTO.getPayPwd());
            if(!user.getPayPassword().equals(md5PayPwd)){
                throw new TransactionOrderException(MessageConstant.PAY_PASSWORD_ERROR);
            }
            if((user.getBalance()-paymentDTO.getAmount())<0){
                throw new PaymentException(MessageConstant.BALANCE_NOT_ENOUGH);
            }else {
                user.setBalance(user.getBalance()-paymentDTO.getAmount());
                userMapper.updateById(user);
                Transaction transaction = transactionMapper.selectById(paymentDTO.getOrderId());
                User seller = userMapper.selectById(transaction.getSellerId());
                seller.setBalance(seller.getBalance()+paymentDTO.getAmount());
                userMapper.updateById(seller);
                transactionMapper.updatePayStatusById(paymentDTO.getOrderId());
            }
            PaymentVO paymentVO = new PaymentVO();
            paymentVO.setQrcodeUrl("支付成功");
            return paymentVO;
        } else {
            throw new TransactionOrderException(MessageConstant.ORDER_NOT_EXIST);
        }
    }
}
